package test.ground.monitor;

import com.wang.tool.monitor.InvokeTemplate;
import com.wang.tool.monitor.core.MonitorInfo;
import com.wang.tool.monitor.template.MonitorInvoker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author wangshaopeng
 * @Date 2022/7/2
 */
@Service
public class ServiceA {

    @Autowired
    private ServiceB serviceB;


//    @Monitor(weaver = SimpleAnnotationWeaver.class/*,weaverConfig = {"pds=key:$[0];key2:$[1]","rds=rs:$.success","fs=$[success == true].errorStatus"}*/)
    public Result hi(String key) {
        return InvokeTemplate.monitor("ServiceA", "hi", new MonitorInvoker<Result>(new Object[]{key,2}) {

            @Override
            public Result internalCall() throws Exception {
//                LinkLogUtil.newEventLog("e1")
//                        .t1("t1")
//                        .log();

//                Thread.sleep(new Random().nextInt(40));
                if(false){
                    throw new IllegalArgumentException("测试异常");
                }
                return new Result("遗产吗");
            }

            @Override
            public void weaveOnBefore(MonitorInfo<Result> monitorInfo) {
//                monitorInfo.addDigest("a", "1");
//                monitorInfo.addDigest("b", null);
                monitorInfo.addTips(monitorInfo.getId()+"1111---");

            }

        });
//        return InvokeTemplate.invoke("1","ServiceA","hi",new MonitorInvoker<Integer>() {
//            @Override
//            public Integer internalCall() throws Exception {
////                throw new IllegalAccessException();
////                LinkLogUtil.newEventLog("testTag134").log();
////                Thread.sleep(new Random().nextInt(20));
////                if(true){
////                    throw new IllegalArgumentException("测试异常");
////                }
//                return 1;
//            }
//
//            @Override
//            public void weaveOnBefore(MonitorInfo<Integer> monitorInfo) {
//                monitorInfo.addDigest("测试摘要","债啊哟");
//                monitorInfo.addDebugExt("test","tetelink");
//
//            }
//
//            @Override
//            public void weaveOnAfter(MonitorInfo<Integer> monitorInfo) {
//
//            }
//        });
//        System.out.println("ServiceA");
//        TimeWatch watch = new TimeWatch();
//        watch.start();
////        EntranceHolder.tryRelease(true);
//
//        for (int i = 0; i < 100000; i++) {
//            boolean b = EntranceHolder.tryMark("1", "3");
//            EntranceHolder.tryRelease(b);
//        }
//        watch.stop();
//        System.out.println(watch.calGlobalCost());
//        System.out.println(EntranceHolder.getEntrance());
//        serviceB.hi();
//        return Integer.parseInt(key);
//        return 1;
    }


    public static class Result{
        private boolean success = true;
        private String errorStatus;

        public Result() {
        }

        public Result(String errorStatus) {
            this.success = errorStatus==null;
            this.errorStatus = errorStatus;
        }

        public boolean isSuccess() {
            return success;
        }

        public void setSuccess(boolean success) {
            this.success = success;
        }

        public String getErrorStatus() {
            return errorStatus;
        }

        public void setErrorStatus(String errorStatus) {
            this.errorStatus = errorStatus;
        }
    }

    public static class Result2 extends Result{

    }


}
